 #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  untitled.py
#  
#  Copyright 2012 Aleksi Joakim Palomäki <aleksi@ajp.dy.fi>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#   

class numfind:
	def numfinder(self, text, string, length):
		import re
		if '<span class="parameter-value">('  != string:
			loc1 = text.find(string)
			unparsed = text[loc1:loc1+length]
			almostparsed = re.findall("[-]?[0-9]*", unparsed)
			parsed = [i for i in almostparsed if i]
			while parsed.count("-") > 0:
				if parsed.count("-") > 0:     #This had extra "-" marks and this is removing the ones we don't need
					#print parsed.count("-")
					parsed.remove("-")
					if parsed.count("-") >= 1: #if we have more then 1 then the - is [0] and bug, remove it.
						parsed.pop(0)
			
			#for i in parsed:
			#	if parsed[0] == "-":
			#		parsed.remove()
				
			
			try: #This is something ugly to define if we have desimals or not :P
				ready = parsed[0] + ',' + parsed[1]
				return ready
			except:
				#if parsed is invalid for above statement we need to set it to something else
				if parsed == []:
					parsed = [-1]
					return parsed[0]
				else: #and in case its not empty [] we still need to set it to something else since it failed above..
					return parsed[0]
		#lets expect its about clouds	
		#TODO: If we ever want to have fiweather work with "All" weather sites,
		# we need smarter system to specify weather we are searching clouds or not..
		else:
			loc1 = text.find(string)
			unparsed = text[loc1:loc1+length]
			almostparsed = re.findall("[-]?[0-9]*", unparsed) 
			parsed = [i for i in almostparsed if i]
			#print unparsed
			while "-" in parsed:
				if "-" in parsed:
					parsed.remove("-")
			if parsed == []:
				parsed = [-1]
				return parsed[0]
			else:
				ready = parsed[0] + '/' + parsed[1]
				return ready
	#print numfinder("moi","kaikenlaistamoimoi teksit ja sit pari numero 349 astetta", "numero", 2000)
